﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HKEA.CMS.Core.IDAL.Tbl;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.DALFactory;
using HKEA.CMS.Core.Utility;
using HKEA.CMS.Core.BLL.Stg;
using System.Transactions;
namespace HKEA.CMS.Core.BLL.Tbl
{
    public class tblAddressBLL
    {
        private  ItblAddress IDAL;
        public tblAddressBLL()
        {
            IDAL = DataAccess.CreateItblAddress();
        }
        /// <summary>
        /// 创建对象
        /// </summary>
        /// <returns></returns>
        public static tblAddressBLL GetInstance()
        {
            return new tblAddressBLL();
        }
        /// <summary>
        /// 插入一条记录
        /// </summary>
        /// <param name="ent"></param>
        /// <returns></returns>
        public int InsertENT(tblAddress ent)
        {
            int ret = 0;
            if (ent != null)
            {
                try
                {
                    ret= IDAL.InsertENT(ent);
                }
                catch (Exception er)
                {
                    
                    throw er;
                }
            }
            return ret;
        }
        /// <summary>
        /// 物理删除
        /// </summary>
        /// <param name="ent"></param>
        /// <returns></returns>
        public int DeleteENT(tblAddress ent)
        {
            int ret = 0;
            if (ent != null)
            {
                try
                {
                    ret = IDAL.DeleteENT(ent);
                }
                catch (Exception)
                {

                    throw;
                }
            }
            return ret;
        }

        /// <summary>
        /// 同步操作
        /// </summary>
        /// <param name="listToUpdate">需要修改的集合</param>
        /// <param name="listStgAddress">需要放入stg_address集合</param>
        /// <returns></returns>
        public bool UpDateAddress(List<tblAddress> listToUpdate, List<stg_tblAddress> listStgAddress)
        {
            bool ret = true;
            if (listToUpdate.Count > 0)
            {
                listStgAddress = listStgAddress.Distinct().ToList();
                listToUpdate = listToUpdate.Distinct().ToList();
                TransactionOptions transactionOption = new TransactionOptions();
                transactionOption.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted;
                transactionOption.Timeout = new TimeSpan(0, 2, 0);

                stg_tblAddressBLL stgAddressBLL = new stg_tblAddressBLL();
                using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, transactionOption))
                {
                    foreach (var temp in listStgAddress)
                    {
                        stg_tblAddress stgAddressENT = new stg_tblAddress();
                        stgAddressENT.AddressUID = temp.AddressUID;
                        var stg = stgAddressBLL.GetEntity(stgAddressENT);
                        if (stg == null)
                        {
                            stgAddressENT.Area_Chi = temp.Area_Chi;
                            stgAddressENT.Area_Eng = temp.Area_Eng;
                            stgAddressENT.City_Chi = temp.City_Chi;
                            stgAddressENT.City_Eng = temp.City_Eng;
                            stgAddressENT.CountryUID = temp.CountryUID;
                            stgAddressENT.District_Chi = temp.District_Chi;
                            stgAddressENT.District_Eng = temp.District_Eng;
                            stgAddressENT.Flat_Bldg_Chi = temp.Flat_Bldg_Chi;
                            stgAddressENT.Flat_Bldg_Eng = temp.Flat_Bldg_Eng;
                            stgAddressENT.FullAdd_Chi = temp.FullAdd_Chi;
                            stgAddressENT.FullAdd_Eng = temp.FullAdd_Eng;
                            stgAddressENT.LastModifyBy = temp.LastModifyBy;
                            stgAddressENT.LastModifyDate = DateTime.Now;
                            stgAddressENT.PostalCode = temp.PostalCode;
                            stgAddressENT.Province_Chi = temp.Province_Chi;
                            stgAddressENT.Province_Eng = temp.Province_Eng;
                            stgAddressENT.Street_Rd_Chi = temp.Street_Rd_Chi;
                            stgAddressENT.Street_Rd_Eng = temp.Street_Rd_Eng;
                            stgAddressENT.AddressUID = temp.AddressUID;
                            stgAddressENT.CreationDate = temp.CreationDate;
                            stgAddressBLL.InsertENT(stgAddressENT);
                        }
                        else
                        {
                            stgAddressENT.Area_Chi = temp.Area_Chi;
                            stgAddressENT.Area_Eng = temp.Area_Eng;
                            stgAddressENT.City_Chi = temp.City_Chi;
                            stgAddressENT.City_Eng = temp.City_Eng;
                            stgAddressENT.CountryUID = temp.CountryUID;
                            stgAddressENT.District_Chi = temp.District_Chi;
                            stgAddressENT.District_Eng = temp.District_Eng;
                            stgAddressENT.Flat_Bldg_Chi = temp.Flat_Bldg_Chi;
                            stgAddressENT.Flat_Bldg_Eng = temp.Flat_Bldg_Eng;
                            stgAddressENT.FullAdd_Chi = temp.FullAdd_Chi;
                            stgAddressENT.FullAdd_Eng = temp.FullAdd_Eng;
                            stgAddressENT.LastModifyBy = temp.LastModifyBy;
                            stgAddressENT.LastModifyDate = DateTime.Now; ;
                            stgAddressENT.PostalCode = temp.PostalCode;
                            stgAddressENT.Province_Chi = temp.Province_Chi;
                            stgAddressENT.Province_Eng = temp.Province_Eng;
                            stgAddressENT.Street_Rd_Chi = temp.Street_Rd_Chi;
                            stgAddressENT.Street_Rd_Eng = temp.Street_Rd_Eng;
                            stgAddressENT.CreationDate = temp.CreationDate;
                            stgAddressBLL.UpdateENT(stgAddressENT);
                        }
                    }
                    foreach (var temp in listToUpdate)
                    {
                        UpdateENT(temp);
                    }
                    try
                    {
                        scope.Complete();
                    }
                    catch (Exception er)
                    { ret = false; }
                }
            }
            return ret;

        }

        public bool UpDateAddress(List<tblAddress> listToRollBack)
        {
            bool ret = true;
            stg_tblAddressBLL stgAddressBLL = new stg_tblAddressBLL();
            TransactionOptions transactionOption = new TransactionOptions();
                transactionOption.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted;
                transactionOption.Timeout = new TimeSpan(0, 2, 0);
                using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, transactionOption))
                {
                    foreach (var temp in listToRollBack)
                    {
                        stg_tblAddress stgAddressENT = new Models.stg_tblAddress();
                        stgAddressENT.AddressUID = temp.AddressUID;
                        var stg = stgAddressBLL.GetEntity(stgAddressENT);
                        if (stg != null)
                        {
                            temp.Area_Chi = stg.Area_Chi;
                            temp.Area_Eng = stg.Area_Eng;
                            temp.City_Chi = stg.City_Chi;
                            temp.City_Eng = stg.City_Eng;
                            temp.CountryUID = stg.CountryUID;
                            temp.District_Chi = stg.District_Chi;
                            temp.District_Eng = stg.District_Eng;
                            temp.Flat_Bldg_Chi = stg.Flat_Bldg_Chi;
                            temp.Flat_Bldg_Eng = stg.Flat_Bldg_Eng;
                            temp.FullAdd_Chi = stg.FullAdd_Chi;
                            temp.FullAdd_Eng = stg.FullAdd_Eng;
                            temp.LastModifyBy = stg.LastModifyBy;
                            temp.LastModifyDate = DateTime.Now;
                            temp.CreationDate = stg.CreationDate;
                            temp.PostalCode = stg.PostalCode;
                            temp.Province_Chi = stg.Province_Chi;
                            temp.Province_Eng = stg.Province_Eng;
                            temp.Street_Rd_Chi = stg.Street_Rd_Chi;
                            temp.Street_Rd_Eng = stg.Street_Rd_Eng;
                            UpdateENT(temp);
                        }
                    }
                    try
                    {
                        scope.Complete();
                    }
                    catch (Exception)
                    { ret = false; throw; }
                }
            return ret;
        }
        /// <summary>
        /// 修改记录
        /// </summary>
        /// <param name="ent"></param>
        /// <returns></returns>
        public int UpdateENT(tblAddress ent)
        {
            int ret = 0;
            if (ent != null)
            {
                try
                {
                    ret = IDAL.UpdateENT(ent);
                }
                catch (Exception er)
                {

                    throw er;
                }
            }
            return ret;
        }
        /// <summary>
        /// 根据条件得到符合条件的结果集，带分页参数
        /// </summary>
        /// <param name="ent"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        public List<tblAddress> GetListByENT(tblAddress ent,PageParam param, string strPropertyName,string strOrderBy)
        {
            List<tblAddress> list=new List<tblAddress>();
            if (ent != null && param != null)
            {
                try
                {
                    list= IDAL.GetListByENT(ent,param,strPropertyName,strOrderBy);
                }
                catch (Exception)
                {
                    throw;
                }
            }
            return list;
        }

        /// <summary>
        /// 根据条件得到符合条件的结果集，不带分页参数
        /// </summary>
        /// <param name="ent"></param>
        /// <returns></returns>
        public List<tblAddress> GetListByENT(tblAddress ent)
        {
            List<tblAddress> list = new List<tblAddress>();
            if (ent != null)
            {
                try
                {
                    list = IDAL.GetListByENT(ent);
                }
                catch (Exception)
                {
                    throw;
                }
            }
            return list;
        }
        public tblAddress GetEntity(tblAddress ent)
        {
            return GetListByENT(ent).FirstOrDefault();
        }
        /// <summary>
        /// 根据条件得到符合条件的记录条数
        /// </summary>
        /// <param name="ent"></param>
        /// <returns></returns>
        public int GetCountByENT(tblAddress ent)
        {
            int ret = 0;
            if (ent != null)
            {
                try
                {
                    ret = IDAL.GetCountByENT(ent);
                }
                catch (Exception)
                {
                    throw;
                }
            }
            return ret;
        }

        
        /// <summary>
        /// 判断是否已存在符合条件的记录
        /// </summary>
        /// <param name="ent"></param>
        /// <returns></returns>
        public bool IsExist(tblAddress ent)
        {
            bool ret = false;
            if (ent != null)
            {
                if (GetCountByENT(ent) > 0)
                    ret = true;
            }
            return ret;
        }

    }
}
